﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommonTagTime.PagerControl;
using CommonTagTime.Business;
using CommonTagTime.ModelObject;
using CommonTagTime;
using System.Resources;

public partial class admin_ContentPageList : SitePage
{
    #region ViewState
    public string F_SortField
    {

        get
        {
            object o = ViewState["F_SortField"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["F_SortField"] = value; }

    }
    public string F_SortExpression
    {

        get
        {
            object o = ViewState["F_SortExpression"];
            if (o != null) return (string)o;
            return string.Empty;
        }

        set { ViewState["F_SortExpression"] = value; }

    }
    public string paraURL
    {
        get
        {
            object o = ViewState["paraURL"];
            if (o != null) return (string)o;
            return string.Empty;
        }
        set { ViewState["paraURL"] = value; }

    }
    #endregion

    #region Init,Loads
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetParamater();
            LoadData();
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);      
    }
    #endregion

    #region Events

    protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            ContentPage objData = (ContentPage)e.Item.DataItem;
            if (objData == null)
                return;
            hidID.Value += objData.PageId.ToString() + ";";
            //image button Edit
            ImageButton imgButton = (ImageButton)e.Item.FindControl("imbDelete");
            imgButton.ToolTip = Resources.Resource.TIP_DELETE;
            imgButton.Attributes.Add("onclick", "return confirm('" + Resources.Resource.MSG_CONFIRM_DELETE + "');");
            imgButton.CommandArgument = objData.PageId.ToString();
            imgButton.ImageUrl = clsString.DELETE_IMAGE_URL;
        }
    }
    protected void rptData_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "SortOrder")
        {
            SortList(e.CommandArgument.ToString());

        }
        else
        {
            int id = clsConvert.Int(e.CommandArgument.ToString());
            if (e.CommandName == "Delete")
                Delete(id);
        }
    }
    private void GetParamater()
    {        
        this.F_SortField = clsQueryString.GetString("F_SortField");
        if (this.F_SortField == string.Empty)
            this.F_SortField = busContentPage.PAGEURL;
        this.F_SortExpression = clsQueryString.GetString("F_SortExpression");
        if (this.F_SortExpression == string.Empty)
            this.F_SortExpression = "ASC";
        
    }
    private void SortList(string F_SortField)
    {
        if (this.F_SortField == F_SortField)
        {
            if (this.F_SortExpression == "ASC")
                this.F_SortExpression = "DESC";
            else this.F_SortExpression = "ASC";
        }
        else
        {
            this.F_SortField = F_SortField;
            this.F_SortExpression = "ASC";
        }
        LoadData();
    }

    protected void btnDelete_Click(object sender, EventArgs e)
    {
        busContentPage objBus = new busContentPage();
        string[] arr = hidIDSelect.Value.Split(';');
        for (int i = 0; i < arr.Length; i++)
        {
            if (arr[i].ToString() != string.Empty)
                objBus.Delete(clsConvert.Int(arr[i].ToString()));
        }
        LoadData();

    }
    protected void btnAddNew_Click(object sender,EventArgs e)
    {
        Response.Redirect("ContentPageDetail.aspx?" + paraURL);
    }

    #endregion

    #region Methods

    private void Delete(int id)
    {
        busContentPage objBus = new busContentPage();
        if (objBus.Delete(id))
            LoadData();
    }
    private void LoadData()
    {
        paraURL = GetPageParams(FilterFieldType.All);       
        hidID.Value = string.Empty;
        //get Condition
        ContentPage objContentPage = new ContentPage();
        objContentPage.OrderBy = this.F_SortField;
        objContentPage.OrderDirection = this.F_SortExpression;
        objContentPage.PageSize = int.MaxValue;
        objContentPage.CurrentPage = 1;
        objContentPage.Condition = " PageUrl NOT LIKE N'%products/%'";
        busContentPage _busContentPage = new busContentPage();
        ContentPageCollection lstResult = _busContentPage.List(objContentPage);      
        if (lstResult != null && lstResult.Count > 0)
        {           
            rptData.DataSource = lstResult;
            divGrid.Visible = true;
            divEmplty.Visible = false;
        }
        else
        {
            rptData.DataSource = null;
            divGrid.Visible = false;
            divEmplty.Visible = true;
            divEmplty.InnerHtml =Resources.Resource.MSG_EMPTY;
        }
        rptData.DataBind();
    }   
    #endregion



}